<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" :form="form">
        <a-row :gutter="24">
          <!-- <a-col :md="6">
            <a-form-item label="归档机构">
              <j-select-depart
                :trigger-change="true"
                v-decorator="['guiDangJiGou',   {
                    rules: [
                      {
                        required: true,
                        message: '请选择归档机构'
                      }
                    ]
                  }]"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="6">
            <a-form-item label="归档年度">
              <j-dict-select-tag
                type="list"
                v-decorator="[
                  'nianDu',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请选择归档年度'
                      }
                    ]
                  }
                ]"
                :trigger-change="true"
                dictCode="NianDu"
                placeholder="请选择归档年度"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="handleSubmit" icon="search">查询</a-button>
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                style="margin-left: 8px"
              >重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>-->
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button @click="baoCun" type="primary">保存</a-button>
      <a-button @click="fanHui" type="primary">返回</a-button>
      <a-input-number placeholder="起始序号" v-model="value" :min="1" @change="onChangeValue" />
    </div>
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="ClearSelected">清空</a>
      </div>
      <a-table
        :columns="columns"
        :data-source="listData"
        bordered
        rowKey="id"
        :loading="loading"
        :pagination="false"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="shangYi(record)">
            <a-icon type="arrow-up" />
          </a>
          <a-divider type="vertical" />
          <a @click="xiaYi(record)">
            <a-icon type="arrow-down" />
          </a>
          <a-divider type="vertical" />
          <a @click="zhiDing(record)">置顶</a>
          <a-divider type="vertical" />
          <a @click="zhiDi(record)">置底</a>
        </span>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
export default {
  name: 'DaJianPXList',
  components: {
    JDate,
    JDictSelectTag,
    JSelectDepart,
  },
  data() {
    return {
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'ciXu',
          width: 100,
          align: 'center',
        },
        {
          title: '件号',
          dataIndex: 'anJuanHao',
          width: 100,
          align: 'center',
          // customRender: function(t, r, index) {
          //   return parseInt(index) + 1
          // }
        },
        {
          title: '题名',
          align: 'center',
          dataIndex: 'tiMing',
        },
        {
          title: '归档年度',
          align: 'center',
          dataIndex: 'nianDu',
        },
        {
          title: '归档机构',
          align: 'center',
          dataIndex: 'jiGouMingCheng',
        },
        {
          title: '保管期限',
          align: 'center',
          dataIndex: 'baoGuanQiXian_dictText',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 200,
          scopedSlots: { customRender: 'action' },
        },
      ],
      listData: [],
      selectedRowKeys: [],
      loading: true,
      value: undefined,
      id: this.$route.query.id,
      isF: true,
      search: {},
      form: this.$form.createForm(this, { name: 'register' }),
      toggleSearchStatus: false,
    }
  },
  created() {
    this.list()
    // this.loading = false
  },
  methods: {
    // 列表
    list(values) {
      getAction(`/daJian/sortList`, { ...values }).then((res) => {
        if (res.code == 200) {
          this.listData = res.result.records
          this.loading = false
        }
      })
    },
    // 复选框
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    // 清空
    ClearSelected() {
      this.selectedRowKeys = []
    },
    // 起始序号
    onChangeValue(value) {
      let data = this.listData
      data.forEach((i, index) => {
        i.ciXu = index + value
      })
      this.listData = data
      console.log('changed', value)
    },
    // 上移
    shangYi(record) {
      let data = this.listData
      data.forEach((i, index) => {
        if (i.id == record.id) {
          if (index == 0) {
            return
          }
          // 序号互换
          record.ciXu = record.ciXu ^ data[index - 1].ciXu
          data[index - 1].ciXu = record.ciXu ^ data[index - 1].ciXu
          record.ciXu = record.ciXu ^ data[index - 1].ciXu
          // 数据互换
          this.swapItems(data, index, index - 1)
          // data[index] = data.splice(index - 1, 1, data[index])[0]
        }
      })
      this.listData = data
    },
    // 下移
    xiaYi(record) {
      let data = this.listData
      let isF = true
      data.forEach((i, index) => {
        if (i.id == record.id && isF == true) {
          if (index == data.length - 1) {
            return
          }
          isF = false
          // 序号互换
          record.ciXu = record.ciXu ^ data[index + 1].ciXu
          data[index + 1].ciXu = record.ciXu ^ data[index + 1].ciXu
          record.ciXu = record.ciXu ^ data[index + 1].ciXu
          // 数据互换
          this.swapItems(data, index, index + 1)
          // data[index] = data.splice(index + 1, 1, data[index])[0]
        }
      })
      this.listData = data
    },
    // 置顶
    zhiDing(record) {
      let data = this.listData
      let key = data[0].ciXu
      data.forEach((i, index) => {
        if (i.id == record.id) {
          if (index == 0) {
            return
          }
          // 序号互换
          // record.ciXu = record.ciXu ^ data[0].ciXu
          // data[0].ciXu = record.ciXu ^ data[0].ciXu
          // record.ciXu = record.ciXu ^ data[0].ciXu
          // 数据互换
          this.swapItems(data, index, 0, 'up')
          // data[index] = data.splice(0, 1, data[index])[0]
        }
      })
      data.forEach((k, index) => {
        k.ciXu = index + key
      })
      this.listData = data
    },
    // 置底
    zhiDi(record) {
      let data = this.listData
      let key = data[0].ciXu
      data.forEach((i, index) => {
        if (i.id == record.id) {
          if (index == data.length - 1) {
            return
          }
          // 序号互换
          // record.ciXu = record.ciXu ^ data[data.length - 1].ciXu
          // data[data.length - 1].ciXu = record.ciXu ^ data[data.length - 1].ciXu
          // record.ciXu = record.ciXu ^ data[data.length - 1].ciXu
          this.swapItems(data, index, 0, 'down')
          // data[index] = data.splice(data.length - 1, 1, data[index])[0]
        }
      })
      data.forEach((k, index) => {
        k.ciXu = index + key
      })
      this.listData = data
    },
    // 置顶方法
    swapItems(arr, index1, index2, direction) {
      if (direction == 'up') {
        //置顶
        arr.unshift(arr[index1])
        arr.splice(index1 + 1, 1)
        return arr
      }
      if (direction == 'down') {
        //置底
        arr.push(arr[index1])
        arr.splice(index1, 1)
        return arr
      }
      arr[index1] = arr.splice(index2, 1, arr[index1])[0]
      return arr
    },
    // 返回
    fanHui() {
      this.$router.push({ path: '/jian' })
    },
    // 保存
    baoCun() {
      let Arr = []
      this.listData.forEach((i, index) => {
        Arr.push({
          id: i.id,
          ciXu: i.ciXu,
        })
      })
      postAction('/daJian/jianSort', Arr).then((res) => {
        if (res.code == 200) {
          this.$message.success(res.message)
          this.list(this.search)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 查询
    handleSubmit(e) {
      this.form.validateFieldsAndScroll((err, values) => {
        if (err) {
          return
        }
        this.search = values
        this.isF = false
        this.list(values)
      })
    },
    // 重置
    searchReset() {
      this.form.resetFields()
      this.listData = []
      this.search = {}
      this.isF = true
    },
    handleToggleSearch() {
      this.toggleSearchStatus = !this.toggleSearchStatus
    },
  },
}
</script>

<style lang="less" scoped>
.search {
  margin-bottom: 54px;
}

.fold {
  width: calc(100% - 216px);
  display: inline-block;
}

.operator {
  margin-bottom: 18px;
}

@media screen and (max-width: 900px) {
  .fold {
    width: 100%;
  }
}
</style>